import axios from "axios";
import router from "@/router/index";
import {auth} from "@/utils/auth";
import "babel-polyfill";
const service = axios.create({
  timeout: 60000, // 请求超时时间
    headers: {
        // Authorization: 'JWT ' + auth.get('token'),
        // header["Authorization"] =  'JWT ' + diao
        // 'Content-Type': 'application/json;charset=UTF-8'
    }
});
service.interceptors.request.use(
  config => {
    // if (auth.tokenExpressInTime() && auth.get('token')) {
      if (auth.get('token')) {
        // console.log("接口token",auth.get('token'))
        config.headers.Authorization = 'JWT ' + auth.get('token')
    }else{
      router.push("/login")
    }
    return config;
  },
  error => {
    console.log("222")
    return Promise.reject(error);
  }
);
service.interceptors.response.use(
  response => { // 该处为后端返回整个内容
    console.log("接口返回",response)
       if(response.data.code == 0 || response.data.code == 2000){
        return response.data
       }else{
        if(response.data.code == 401){
          router.push("/login")
        }
        return response
       }
      //  return response.data
  },
  error => {
    console.log(error)
       return Promise.reject(error)
  }

);
// service.defaults.withCredentials = false; //意思是携带cookie信息,保持session的一致性
export default service;
